package com.syni.mdd.yhd.common.all.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.util.CollectionUtils;

public class MapUtils extends org.apache.commons.collections.MapUtils {

    public static void transferValueIfNotNull(Map<String, Object> origin, Map<String, Object> target, String key) {
        if (origin.containsKey(key) && origin.get(key) != null) {
            target.put(key, origin.get(key));
        }
    }
    
    public static void transferObj2String(Map<Object, Object> origin, Map<String, String> target) {
    	if(origin == null){
    		return;
    	}
    	origin.keySet().forEach(key -> {
    		 if (origin.containsKey(key) && origin.get(key) != null) {
    	            target.put(key.toString(), getString(origin, key));
    	       }
    	});
    }

    public static boolean checkNull(Map<String, Object> map, String key) {
        if (!map.containsKey(key)) {
            return true;
        }
        if (map.get(key) == null || StringUtils.isBlank(map.get(key).toString())) {
            return true;
        }
        return false;
    }

    public static double divValue(Map map, Map map2, String key) {
        if(map2==null||map==null)return 0;
        String s1 =(String) map.get(key);
        String s2 =(String) map2.get(key);
        if(!NumberUtils.isNumber(s1)){
            s1="0";
        }
        if(!NumberUtils.isNumber(s2)){
            s2="0";
        }
        double d1 = NumberUtils.createDouble(s1);
        double d2 = NumberUtils.createDouble(s2);
        if (d2 <= 0 && d1 > 0) {
            return 1.0;
        } else if (d2 <= 0 && d1 <= 0) {
            return 0;
        } else {
            double result = (d1 - d2) / d2;
            return Double.parseDouble(String.format("%.2f", result * 100));
        }
    }

    public static Map addInMap(Object key, Object value) {
        Map map = new HashMap();
        map.put("key", key);
        map.put("value", value);
        return map;
    }
    public static Map<String,Object> getDateStrMap(List<Map<String,Object>> list , String str){
        Map<String,Object> returnMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(list)){
            list.forEach((k)->{
                 Map<String,Object> map = k;
                 String dateStr = (String)k.get("dateStr");
                 if(str.equals(dateStr)){
//                     returnMap=map;
                     returnMap.putAll(map);
                     return ;
                 }
             });
         }
         return returnMap;
    }

    public static String toString(Map<String,String> map){
    	StringBuilder sb = new StringBuilder();
    	sb.append("{'");
    	map.forEach((k,v) -> {
    		sb.append(k).append("':").append("'").append(v).append("';");
    	});
    	return sb.substring(0, sb.length()-1)+"}";
    }

    public static Map<String,String> toStringJsonMap(Map<String,Object> conditionMap){
        Map<String,String> stringStringMap = new HashMap<>();
        if(conditionMap.size() > 0){
            for(Map.Entry<String,Object> objectEntry : conditionMap.entrySet()){
                stringStringMap.put(objectEntry.getKey(), JSON.toJSONString(objectEntry.getValue()));
            }
        }
        return stringStringMap;
    }
    
    
}
